#include "Items.h"
#include "Engine\Engine.h"
#include <fstream>
#include <assert.h>
#include <string>
#include <stdio.h>
Item_Base::Item_Base():BaseObject()
{
	uniqueID		= -1;
	skillAlignment = skillRequisite = 0;
	itemName		= "INVALID_REFERENCE";
	objectType		= TYPE_ENTITY_PIECE;		
	active			= true;
}
void Item_Base::_itemInit(short type, short skill, short requisite, char* name, Vector2D* ASSET)
{
	itemName		= "INVALID_REFERENCE";
	if(ASSET == NULL)
		iconAsset = Engine::Ins()->graphics.AddAsset("Resources/Meshes/weapon_block.X","Resources/Sprites/Alpha.png", NULL, NULL, ASSET_WORLD);
	else
		iconAsset = *ASSET;
	itemType		= type;
	itemName		= name;
	skillAlignment	= skill;
	skillRequisite	= requisite;
}

void Item_Base::_recipeInit(char* FileName, recipe* a_Recipe)
{
	objectType		= TYPE_ENTITY_PIECE;
	active			= true;

	ifstream File(FileName);
	if(File.is_open())
	{
		string trashData;
		string goodData;

		for(int i = 0; i < 4; i++)
		{
			getline(File,trashData,':');
			getline(File,goodData,';');
			a_Recipe->namIngredients[i] = wordModify::copy(a_Recipe->namIngredients[i],goodData);
		}
	}
}

void Item_Base::set(Item_Base i)
{
	BaseObject::set((BaseObject)i);
	iconAsset		= i.iconAsset;
	itemType		= i.itemType;
	itemName		= wordModify::copy(i.itemName);
	skillAlignment	= i.skillAlignment;
	skillRequisite	= i.skillRequisite;

}

void Item_Base::serialize(ByteBuffer*& info)
{
	int strSize;	// for storing the lengths of strings
	BaseObject::serialize(info);

	// don't need to store as floats because asset indices should be whole values
	info->addLong((long)iconAsset.x);
	info->addLong((long)iconAsset.y);
	info->addShort(itemType);
	info->addShort(NULL);

	strSize = strLength(itemName)+1;
	info->addLong(strSize);
	info->addBytes((const unsigned char*)itemName, strSize);

	info->addShort(skillAlignment);
	info->addShort(skillRequisite);
}

void Item_Base::deserialize(ByteBuffer* info, int & index)
{
	const int lgSize = sizeof(long);
	const int shrtSize = sizeof(short);
	int strSize;

	BaseObject::deserialize(info, index);

	iconAsset.x = (float)info->getLong(index); index += lgSize;
	iconAsset.y = (float)info->getLong(index); index += lgSize;

	itemType = (short)info->getShort(index); index += shrtSize;
	short deleteMe = (short)info->getShort(index); index += shrtSize;

	strSize= info->getLong(index); index += lgSize;
	itemName = new char[strSize];
	for(int i = 0; i < strSize; ++i)
		itemName[i] = info->getByte(index++);

	skillAlignment = info->getShort(index); index += shrtSize;
	skillRequisite = info->getShort(index); index += shrtSize;
}